/**
 * @class 彗星实体
 * @author DuskyHuang 圣子
*/

import { smoothstep } from 'algorithms';
import { _decorator, Component, Material, renderer, Color } from 'cc';
import { core } from 'cfw';
import { Res } from 'const';
import { planetary } from 'proxies';
import { Comet } from './comet';
const { ccclass, property } = _decorator;

@ccclass('cocosmos.comet-hd')
export class CometHightDefinition extends Component {

    @property(Material) public sharedInstMaterial: Material;

    protected onLoad(): void {
        this._sharedPass0 = this.sharedInstMaterial.passes[0];
        this._sharedHandle = this._sharedPass0.getHandle("nearStrength");
    }

    protected onEnable(): void {
        const { centerName } = planetary;
        const config = core.res.getConfigItem<cometConfig>(Res.Configs.SOLAR_COMETS, centerName);
        if (config?.tint) Color.fromHEX(color, config?.tint);
        else Color.copy(color, DFT_COL);
        this.sharedInstMaterial.setProperty('baseColor', color);
    }

    protected update(): void {
        this._syncToCenter();
        this._sharedPass0.setUniform(this._sharedHandle, this._strength);
    }

    private _syncToCenter(): void {
        const { center } = planetary;
        if (center && center instanceof Comet) {
            const comet = center;
            this.node.setWorldScale(comet.node.worldScale);
            this.node.setWorldPosition(comet.node.worldPosition);
            this._strength = smoothstep(0.5, 0.1, comet.node.worldScale.x);
            comet.faceToSun(this.node);
        }
    }

    private _strength: num.pct = 1;
    private _sharedPass0: renderer.Pass;
    private _sharedHandle: handle;
}

const color = new Color(255, 255, 255, 255);
const DFT_COL = new Color(0, 97, 191, 255);



// ,"tint": "#6199ff"
// ,"tint": "#3e6ffc"
//
// ,"tint": "#8d2be7"
//
// ,"tint": "#1ecbea"
// ,"tint": "#2c6dff"
//
// ,"tint": "#2dc1ff"
//
// ,"tint": "#4efff0"
//
//
// ,"tint": "#703df0"
// ,"tint": "#52e6a6"
//
// ,"tint": "#3a92ff"
// ,"tint": "#3ab3ff"
// ,"tint": "#35ff5b"
// ,"tint": "#4bdd4b"
//
//
// ,"tint": "#a8ff5b"
//
// ,"tint": "#a6ff3c"
// ,"tint": "#ecf55a"